from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from rest_framework import filters

from apps.projects.models import Project, ProjectAppeal
from apps.projects.serializers import ProjectSerializer, ProjectAppealSerializer
from apps.projects.filters import ProjectFilter, ProjectAppealFilter
from utils.base import BasePageNumberPagination
from utils.base_view import BaseModelViewSet


class ProjectViewSet(BaseModelViewSet):
    queryset = Project.objects.all()
    serializer_class = ProjectSerializer
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = ProjectFilter


class ProjectAppealViewSet(BaseModelViewSet):
    queryset = ProjectAppeal.objects.all()
    serializer_class = ProjectAppealSerializer
    permission_classes = [IsAuthenticated]
    pagination_class = BasePageNumberPagination
    filterset_class = ProjectAppealFilter

    def update(self, request, *args, **kwargs):
        response = super().update(request, *args, **kwargs)
        project_id = request.data['project']
        user_id = request.data['user']
        project_obj = Project.objects.all().get(id=project_id)
        users = list(project_obj.user.all().values_list('id', flat=True))
        users.append(user_id)
        project_obj.user.set(users)
        project_obj.save()
        return response



